package org.example.Server.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.Server.utils.AliOSSUtils;
import org.example.Server.utils.ResultAPI;
import org.example.Server.utils.Upload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@Slf4j
@RequestMapping("/youxich/upload")
public class UploadController {
    @Autowired
    AliOSSUtils aliOSSUtils;

    @PostMapping
    public ResultAPI upload(@RequestParam("file") MultipartFile file, @RequestParam(defaultValue = "1") Integer status) {
        String url = null;
        switch (status) {
            case 1:
                url = Upload.upload(file, "avator/");
                break;
            case 2:
                url = Upload.upload(file, "homework/");
                break;
            case 3:
                url = Upload.upload(file, "cover/");
                break;
        }
//        try {
//            url = aliOSSUtils.upload(file);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
        return ResultAPI.success(url);
    }
}
